#include "stm32f10x.h"
#include "init.h"
#include "SD.h"
#include "uart.h"
#include "GPS.h"
#include "GPRS.h"
#include "flash.h"
#include "stm32f10x_iwdg.h"
#include "stm32f10x_spi.h"
#include <stdio.h>

extern	u8  Time_Over_Z;
extern	u8  Time_Over_W;
extern	u8  Buzzer;
extern	u8  Stop_Time_Over;
extern  u8 Speech_Flag;
/************************************
函数功能：GPIO口配置
函数入口：无
函数出口：无
函数说明：		 
***********************************/
void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
					/*G600-复位脚*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
			 /*PB6-蜂鸣器,PB7-继电器*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;	     
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
					  //PA1
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //初始化 IO 口为输入。
	GPIO_Init(GPIOB, &GPIO_InitStructure); 
 	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource7);	 

	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB,&GPIO_InitStructure);

				   /*PA11-指示灯*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_1|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_11; // Modify by yc 20160703
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	/*SD卡语音播放引脚配置 PA8-DI,PB13-RST,PB14-CLK,PB15-CS */
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB,&GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	/* Configure USART1 Tx (PA.09) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/* Configure USART2 Tx (PA2) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	/* Configure USART3 Tx (PB10) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	/* Configure USART1 Rx (PA.10) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/* Configure USART2 Rx (PA3) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	/* Configure USART3 Rx (PB11) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

		/* 配置 SPI1 */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

		/* 配置 SPI1 */
//	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6;
//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); 
	GPIO_PinRemapConfig(GPIO_Remap_SPI1,ENABLE);
	// GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);

}
/************************************
函数功能：系统中断配置
函数入口：无
函数出口：无
函数说明：		 
***********************************/
void NVIC_Configuration(void)
{

  NVIC_InitTypeDef NVIC_InitStructure; 
  EXTI_InitTypeDef EXTI_InitStructure;

  /* Configure the NVIC Preemption Priority Bits */  
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  
  /* Enable the USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

     /* Enable the USART2 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

       /* Enable the USART3 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

 // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; 
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  NVIC_Init(&NVIC_InitStructure); 

 // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  NVIC_Init(&NVIC_InitStructure); 

	/* SP11 优先级配置 */
	NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure); 
  	

//	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitStructure.NVIC_IRQChannel =EXTI9_5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =3; 
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);	
	
	EXTI_InitStructure.EXTI_Line = EXTI_Line7;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;	   
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);	 
}


/************************************
函数功能：系统时钟配置,使能各外设时钟
函数入口：无
函数出口：无
函数说明：		 
***********************************/
void RCC_Configuration(void)
{

	SystemInit();	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOD 
						  |RCC_APB2Periph_AFIO  | RCC_APB2Periph_USART1, ENABLE );

 	 /* Enable USART2 clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3|RCC_APB1Periph_TIM2
						  |RCC_APB1Periph_TIM3, ENABLE);

	RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE);

}
/************************************
函数功能：定时器初始化
函数入口：arr,计数器自动重装值，psc 预分频器数
函数出口：无
函数说明：Timer2_Init(10000,7199)，为1s中断一次，测试验证过	 
			可以设置5s中断一次，不过10s就不行了
***********************************/
void Timer2_Init(u16 arr,u16 psc)
{
//	RCC->APB1ENR|=1<<1;//TIM3时钟使能    
 	TIM2->ARR=arr;  //设定计数器自动重装值//刚好1ms    
	TIM2->PSC=psc;  //预分频器7200,得到10Khz的计数时钟
	//这两个东东要同时设置才可以使用中断
	TIM2->DIER|=1<<0;   //允许更新中断				
	TIM2->DIER|=1<<6;   //允许触发中断	  							    
//	TIM2->CR1|=0x0001;    //使能定时器3
	TIM2->CR1&=0x03FE;	//失能定时器3
  									 
}
/************************************
函数功能：定时器初始化
函数入口：arr,计数器自动重装值，psc 预分频器数
函数出口：无
函数说明：Timer3_Init(10000,7199)，为1s中断一次，测试验证过	 
			可以设置5s中断一次，不过10s就不行了
***********************************/
void Timer3_Init(u16 arr,u16 psc)
{
//	RCC->APB1ENR|=1<<1;//TIM3时钟使能    
 	TIM3->ARR=arr;  //设定计数器自动重装值//刚好1ms    
	TIM3->PSC=psc;  //预分频器7200,得到10Khz的计数时钟
	//这两个东东要同时设置才可以使用中断
	TIM3->DIER|=1<<0;   //允许更新中断				
	TIM3->DIER|=1<<6;   //允许触发中断	  							    
//	TIM3->CR1|=0x0001;    //使能定时器3
	TIM3->CR1&=0x03FE;	//失能定时器3
  									 
}

/******************************************************************
** 函数名：		 SPI初始化
** 函数功能：    SPI各参数配置
** 输入参数：    SPI_Mode_Master,SPI_Direction_1Line_Tx
** 返回值：      无
** 函数创建者：  乐 强
** 函数创建日期：2013-09-11
** 函数说明：  SPI_CR2寄存器SSOE位为1，且处于主模式，NSS输出低电平
**-----------------------------------------------------------------
** 函数修改者：		
** 函数修改日期：  
******************************************************************/	

void SPIx_Init(SPI_TypeDef* SPIx,uint16_t SPI_Mode , uint16_t SPI_Direction)
{
	/* SPI初始化配置 */
  SPI_InitTypeDef   SPI_InitStructure;

  SPI_InitStructure.SPI_Direction = SPI_Direction;
  SPI_InitStructure.SPI_Mode = SPI_Mode ;		  //当SSI=1时，为主模式，当SSI=0;时，为从模式 ，软件模式下NSS=SSI
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;			 //软件模式时，SSM=1，硬件模式时，SSM=0;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPIx, &SPI_InitStructure);

  SPI_I2S_ITConfig(SPIx,SPI_I2S_IT_RXNE, ENABLE);
  SPI_Cmd(SPIx, ENABLE);

}

 #if 0
void SPIx_Init(SPI_TypeDef* SPIx,uint16_t SPI_Mode , uint16_t SPI_Direction)
{
	/* SPI初始化配置 */
  SPI_InitTypeDef   SPI_InitStructure;

  SPI_InitStructure.SPI_Direction = SPI_Direction;
  SPI_InitStructure.SPI_Mode = SPI_Mode ;		  //当SSI=1时，为主模式，当SSI=0;时，为从模式 ，软件模式下NSS=SSI
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;			 //软件模式时，SSM=1，硬件模式时，SSM=0;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
 // SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPIx, &SPI_InitStructure);

  SPI_I2S_ITConfig(SPIx,SPI_I2S_IT_RXNE, ENABLE);
  SPI_Cmd(SPIx, ENABLE);

}
#endif
/************************************
函数功能：所有外设配置
函数入口：无
函数出口：无
函数说明：		 
***********************************/
void Init_All_Periph(void)
{
	RCC_Configuration();   //时钟配置	
	GPIO_Configuration();  //GPIO口配置
	NVIC_Configuration();  //系统中断向量配置

	GPIO_SetBits(GPIOB,GPIO_Pin_9);	 //关闭蜂鸣器
	GPIO_SetBits(GPIOA,GPIO_Pin_11);	    //信号灯
	GPIO_SetBits(GPIOA,GPIO_Pin_1);	   		//GPS指示灯
	
	GPIO_SetBits(GPIOB,GPIO_Pin_12); 	// 4V电源

	USART1_Configuration();	//串口1配置
	USART2_Configuration();	//串口2配置
	USART3_Configuration();	//串口2配置
	
   USART_ClearFlag(USART1,USART_FLAG_TC); //清除标志位，否则第1位数据会丢失
   USART_ClearFlag(USART2,USART_FLAG_TC);
   USART_ClearFlag(USART3,USART_FLAG_TC);

	 SD_Init();					//SD卡语音播放初始化
	 Timer2_Init(10000,7199);	// 定时1秒钟，产生一次中断
	 Timer3_Init(10000,7199);	// 定时1秒钟，产生一次中断

	 Flash_Init_Write();	//坐标点修改
	 Flash_Init_Read();	//坐标点初始化

	 SPIx_Init(SPI1,SPI_Mode_Slave,SPI_Direction_1Line_Rx);
		
//	GPIO_ResetBits(GPIOA,GPIO_Pin_5);	  
	GPIO_SetBits(GPIOA,GPIO_Pin_5);	 //指示灯
//	GPIO_ResetBits(GPIOA,GPIO_Pin_6);	  
	GPIO_SetBits(GPIOA,GPIO_Pin_6);	 //指示灯
//	GPIO_ResetBits(GPIOA,GPIO_Pin_1);	  
	GPIO_SetBits(GPIOA,GPIO_Pin_1);	 //指示灯

//	GPIO_ResetBits(GPIOB,GPIO_Pin_9);	  
	GPIO_SetBits(GPIOB,GPIO_Pin_9);	

//	Send_Command(0x00);
//  Send_Command(0x03);

									  
	GPIO_ResetBits(GPIOA,GPIO_Pin_4);	  //GSM启动
 	Statu_Flag=0;
	GPRS_Init_OK=0;
	Function=5;	
	GPS_TO_GPRS_Data();					//先将GPS数据转化  	

	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

//	UART3_SendString("hello");
    printf("Hello\n");

}

/**************************************************
函数功能：看门狗配置
函数入口：无
函数出口：无
函数说明  预分频系数		最长时间（ms）0x0fff
			/4					409.6
			/8					819.2
			/16					1638.4
			/32					3276.8
			/64					6553.6
			/128				13107.2
			/256				26214.4
编写时间：2012-09-06
编写作者：乐 强
**************************************************/
void IWDG_int(void)
{
  IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);	//使能IWDG_PR和IWDG_RLR的写操作
  IWDG_SetPrescaler(IWDG_Prescaler_128);//设置IWDG预分频值为32最小的时间为
  IWDG_SetReload(0x0FFF);//设置IWDG重装载值为1s
  IWDG_ReloadCounter();//将重装载值装载到IWDG计数器中
  IWDG_Enable();//使能IWDG  			   
}

//外部中断7服务程序
void EXTI9_5_IRQHandler(void)
{
	
   if(EXTI_GetITStatus(EXTI_Line7)!=RESET)//如果8号线有中断到来
	{
	    EXTI_ClearITPendingBit(EXTI_Line7); 
		if(Speech_Flag)
		{
			Speech_Flag=0;	
			if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7))
			{				
		   		GPIO_ResetBits(GPIOB,GPIO_Pin_6);	   //电源继电器
					Delay1ms(20);
				GPIO_ResetBits(GPIOB,GPIO_Pin_8);	   //输出继电器		
			}				
		}
	}
} 

void TIM2_IRQHandler(void)
{ 		    		  			    
	if(TIM2->SR&0X0001)//溢出中断
	{				
		Time_Over_W--;	   				   				     	    	
	}						   
	TIM2->SR&=~(1<<0);//清除中断标志位 	    
}

void TIM3_IRQHandler(void)
{ 		    		  			    
	if(TIM3->SR&0X0001)//溢出中断
	{
		Time_Over_Z--;				    				   				     	    	
	}				   
	TIM3->SR&=~(1<<0);//清除中断标志位 	    
}


